package blow.sky.quoted.controller;

import blow.sky.quoted.dao.UserDao;
import blow.sky.quoted.entity.QuoteUser;
import blow.sky.quoted.request.AccountRequest;
import blow.sky.quoted.response.AccountResponse;
import blow.sky.quoted.utils.BearTokenUtil;
import blow.sky.quoted.utils.EmailContent;
import blow.sky.quoted.utils.EmailUtil;
import blow.sky.quoted.utils.VerifyCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Optional;

/**
 * @Description 账户相关的路由
 * @Author 孙焰明
 * @Date 2021/5/20 11:07
 */

@Validated
@RestController
@RequestMapping("/api/account")
public class AccountController
{
	@Autowired
	private UserDao dao;
	@Autowired
	private EmailUtil emailUtil;
	@Autowired
	private VerifyCodeUtil verifyCodeUtil;
	@Autowired
	private BearTokenUtil tokenUtil;
	@Value("${verify-code.time}")
	private int expireTime;

	// 获取验证码
	@PostMapping(value = "/code")
	public AccountResponse getCode(@Valid @RequestBody AccountRequest codeRequest, HttpServletRequest request, HttpServletResponse response)
	{
		EmailContent emailContent = new EmailContent();
		emailContent.setTo(codeRequest.getEmail());
		emailContent.setSubject("Verify Your Email To Complete Register QuotedPrice System");
		String code = verifyCodeUtil.createCode(codeRequest.getEmail());
		emailContent.setContent("Your Verify Code is: <strong>" + code + "<strong> .");
		emailUtil.sendHTMLEmail(emailContent);
		return new AccountResponse("success", "Verify Code send to " + codeRequest.getEmail() + ", Valid in " + expireTime + " minutes.");
	}

	// 注册
	@PostMapping(value = "/register")
	public AccountResponse register(@Valid @RequestBody AccountRequest user, HttpServletRequest request, HttpServletResponse response)
	{
		Optional<QuoteUser> inUser = dao.findById(user.getEmail());
		// 如果邮箱未注册
		if (!inUser.isPresent())
		{
			if (verifyCodeUtil.verifyCode(user.getEmail(), user.getCode()))
			{
				QuoteUser quoteUser = new QuoteUser(user.getEmail(), user.getPassword());
				dao.saveAndFlush(quoteUser);
				return new AccountResponse("success");
			}
			return new AccountResponse("fail", "Verify Code Fail");
		}
		return new AccountResponse("fail", "Email has been register");
	}

	@PostMapping(value = "/login")
	public AccountResponse login(@Valid @RequestBody QuoteUser user, HttpServletRequest request, HttpServletResponse response)
	{
		Optional<QuoteUser> inUser = dao.findById(user.getEmail());
		if (inUser.isPresent())
		{
			if (inUser.get().getPassword().equals(user.getPassword()))
			{
				String token = tokenUtil.createToken(user.getEmail());
				return new AccountResponse("success", null, token);
			}
			return new AccountResponse("fail", "Password is wrong");
		}
		return new AccountResponse("fail", "Email not registered yet");
	}

	@PostMapping(value = "/password")
	public AccountResponse changePassword(@Valid @RequestBody AccountRequest user, HttpServletRequest request, HttpServletResponse response)
	{
		Optional<QuoteUser> inUser = dao.findById(user.getEmail());
		if (inUser.isPresent())
		{
			QuoteUser newUser = inUser.get();
			if (verifyCodeUtil.verifyCode(user.getEmail(), user.getCode()))
			{
				newUser.setPassword(user.getPassword());
				dao.saveAndFlush(newUser);
				return new AccountResponse("success");
			}
		}
		return new AccountResponse("fail", "Email not registered yet");
	}
}
